package com.lite.api.controller.publics.accessory;


import com.lite.business.convert.accessory.AccessoryConvert;
import com.lite.business.entity.accessory.Accessory;
import com.lite.business.exception.BusinessException;
import com.lite.business.service.accessory.AccessoryService;
import com.lite.common.dto.ResultResponse;
import com.lite.common.utils.ResultResponseUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author stranger
 * @since 2022-10-24
 */
@RestController
@Validated
@RequestMapping("/accessory")
public class AccessoryController {

    @Autowired
    AccessoryService accessoryService;

    @Autowired
    AccessoryConvert accessoryConvert;


    /**
     * 获取一个商品附属品的信息
     * @param cmoId 商品ID
     * @param accessoryId 附属品ID
     * @return 附属品
     * @throws BusinessException 业务异常
     */
    @GetMapping("/getAccessoryInfoById")
    public ResultResponse<Accessory> getAccessoryInfoById(
            @RequestParam @NotNull Integer cmoId, @RequestParam @NotNull Integer accessoryId) throws BusinessException {
        return ResultResponseUtils.success(accessoryService.getAccessoryInfoById(cmoId, accessoryId), "获取商品附属品信息成功");
    }

    /**
     * 获取一个商品的附属品信息列表
     * @param cmodId 商品ID
     * @param page 页数
     * @param size 容量
     * @return 附属品列表
     * @throws BusinessException 业务异常
     */
    @PostMapping("/listAccessoryByPage")
    public ResultResponse<List<Accessory>> listAccessoryByPage(
            @RequestParam @NotNull Integer cmodId, @RequestParam @NotNull Integer page, @RequestParam @NotNull Integer size) throws BusinessException {
        return ResultResponseUtils.success(accessoryService.listAccessoryByPage(cmodId, page, size), "获取商品附属品列表信息成功");
    }

    /**
     * 更新一个附属品信息
     * @param cmodId 商品ID
     * @param accessoryId 附属品ID
     * @param newAccessory 新的附属品对象
     * @return 是否更新成功
     * @throws BusinessException 业务异常
     */
    @PostMapping("/updateAccessoryInfoById")
    public ResultResponse<Boolean> updateAccessoryInfoById(
            @RequestParam @NotNull Integer cmodId, @RequestParam @NotNull Integer accessoryId, @RequestParam @NotNull Accessory newAccessory) throws BusinessException {
        return ResultResponseUtils.success(accessoryService.updateAccessoryInfoById(cmodId, accessoryId, newAccessory), "更新附属品信息成功");
    }

    /**
     * 删除一个附属品信息
     * @param cmodId 商品ID
     * @param accessoryId 附属品ID
     * @return 是否删除成功
     * @throws BusinessException 业务异常
     */
    @DeleteMapping("/removeAccessoryInfoById")
    public ResultResponse<Boolean> removeAccessoryInfoById(
            @RequestParam @NotNull Integer cmodId, @RequestParam @NotNull Integer accessoryId) throws BusinessException {
        return ResultResponseUtils.success(accessoryService.removeAccessoryInfoById(cmodId, accessoryId), "删除附属品信息成功");
    }

    /**
     * 新增一个商品附属品信息
     * @param accessory 新的附属品对象
     * @return 是否新增成功
     * @throws BusinessException 业务异常
     */
    @PostMapping("/addAccessoryInfo")
    public ResultResponse<Boolean> addAccessoryInfo(@RequestBody Accessory accessory) throws BusinessException {
        return ResultResponseUtils.success(accessoryService.addAccessoryInfo(accessory), "新增商品附属品信息成功");
    }
}
